package com.tac.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tac.common.R;
import com.tac.dto.ProductDto;
import com.tac.pojo.Kind;
import com.tac.pojo.Product;
import com.tac.service.KindService;
import com.tac.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/product")
@CrossOrigin
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private KindService kindService;
    /**
     * 添加商品
     *
     * @param product
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Product product) {
        Integer amout = product.getAmout();
        if(amout!=null){
            if (amout > 0) {
                productService.save(product);
                return R.success(null, "添加成功");
            }
        }
        return R.error("库存必须大于0");
    }

    /**
     * 逻辑删除
     *
     * @param pid
     * @return
     */
    @DeleteMapping("/delete")
    public R delete(Integer pid) {
        productService.removeById(pid);
        return R.success(null, "删除成功");
    }

    /**
     * 修改   商品货号不能改
     *
     * @param product
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody Product product) {
        Integer id = product.getId();
        Product product1 = productService.getById(id);
        String code = product.getCode();
        if(product1.getCode().equals(code)){
            Integer amout = product.getAmout();
            if(amout!=null){
                if (amout > 0) {
                    productService.updateById(product);
                    return R.success(null,"修改成功");
                }

            }
            return R.error("库存必须大于0");
        }else {
            return R.error("商品货号不能修改");
        }
    }

    /**
     * 查看分类商品
     * @param kid
     * @return
     */
    @GetMapping("/selectByKindId")
    public R selectByKindId(Integer kid){
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(kid!=null,Product::getKind,kid);
        List<Product> list = productService.list(queryWrapper);

        ArrayList<ProductDto> arrayList = new ArrayList<>();
        for(Product product:list){
            ProductDto productDto = new ProductDto();
            BeanUtils.copyProperties(product,productDto);
            Integer kindid = product.getKind();
            Kind kind = kindService.getById(kindid);
            productDto.setKindName(kind.getName());
            arrayList.add(productDto);
        }

        return R.success(arrayList,"success");
    }

    /**
     * 查看单个商品信息详情
     * @param id
     * @return
     */
    @GetMapping("/selectByid")
    public R selectByid(Integer id){
        Product product = productService.getById(id);
        if(product==null){
           return R.error("商品不存在");
        }
        ProductDto productDto = new ProductDto();
        BeanUtils.copyProperties(product,productDto);
        Kind kind = kindService.getById(product.getKind());
        productDto.setKindName(kind.getName());
        return R.success(productDto,"success");
    }

}
